﻿using System;
using System.Text;
using System.Web;
using Me.Amon.C;
using Me.Amon.CRM.M;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.CRM;
using Me.Amon.Da.Db.POS;
using Me.Amon.POS.C;
using Me.Amon.POS.M;
using Newtonsoft.Json;

namespace Me.Amon.POS
{
    /// <summary>
    /// Pos 的摘要说明
    /// </summary>
    public class Pos : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.ContentEncoding = Encoding.UTF8;

            var obj = new MTerminal();

            #region 操作参数
            var opt = context.Request[CObj.PARAM_KEY_OPT];
            if (string.IsNullOrEmpty(opt))
            {
                obj.error_code = PObj.ERROR_CODE_OPT_EMPTY;
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }
            var corp = context.Request[PPos.PARAM_KEY_CORP];
            if (string.IsNullOrWhiteSpace(corp))
            {
                obj.error_code = PPos.ERROR_CODE_CORP_EMPTY;
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }
            var shop = context.Request[PPos.PARAM_KEY_SHOP];
            if (string.IsNullOrWhiteSpace(shop))
            {
                obj.error_code = PPos.ERROR_CODE_SHOP_EMPTY;
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }
            var code = context.Request["t"];
            if (string.IsNullOrWhiteSpace(code))
            {
                obj.error_code = PPos.ERROR_CODE_DAT_EMPTY;
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }
            var addr = context.Request["a"];
            if (string.IsNullOrWhiteSpace(addr))
            {
                obj.error_code = PPos.ERROR_CODE_CUSTOM;
                obj.error_message = "客户机地址不能为空！";
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }
            #endregion

            #region 信息读取
            var dba = new RDBEngine();
            var corpObj = ReadCorp(dba, corp, obj);
            if (corpObj == null)
            {
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }

            dba.Reset();
            var shopObj = ReadShop(dba, corp, shop, obj);
            if (shopObj == null)
            {
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }

            dba.Reset();
            if (!ReadTerminal(dba, corp, shop, code, addr, obj))
            {
                context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
                context.Response.End();
                return;
            }
            #endregion

            obj.corp = corpObj;
            obj.shop = shopObj;

            context.Response.Write(JsonConvert.SerializeObject(obj, Formatting.None));
            context.Response.End();
        }

        private MCompany ReadCorp(RDBEngine dba, string corp, MTerminal obj)
        {
            dba.AddTable(DCompany.TABLE);
            dba.AddWhere(DCompany.CODE, corp);
            dba.AddWhere(DCompany.STATUS, CPos.STATUE_ENABLED.ToString());

            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count < 1)
            {
                obj.error_code = PObj.ERROR_CODE_CUSTOM;
                obj.error_message = "无效的公司代码！";
                return null;
            }

            var tmp = new MCompany();
            var row = dt.Rows[0];
            tmp.id = row[DCompany.ID] as string;
            tmp.code = row[DCompany.CODE] as string;
            tmp.name = row[DCompany.NAME] as string;
            tmp.address = row[DCompany.ADDRESS] as string;
            tmp.postcode = row[DCompany.POSTCODE] as string;
            tmp.homepage = row[DCompany.HOMEPAGE] as string;
            tmp.contact = row[DCompany.CONTACT] as string;
            tmp.telephone = row[DCompany.TELEPHONE] as string;
            tmp.status = CPos.STATUE_ENABLED;
            obj.corp = tmp;

            return tmp;
        }

        private MShop ReadShop(RDBEngine dba, string corp, string shop, MTerminal obj)
        {
            dba.AddTable(DShop.TABLE);
            dba.AddWhere(DShop.CORP_ID, corp);
            dba.AddWhere(DShop.CODE, shop);
            dba.AddWhere(DShop.STATUS, CPos.STATUE_ENABLED.ToString());

            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count < 1)
            {
                obj.error_code = PObj.ERROR_CODE_CUSTOM;
                obj.error_message = "无效的门店代码！";
                return null;
            }

            var tmp = new MShop();
            var row = dt.Rows[0];
            tmp.id = row[DShop.ID] as string;
            tmp.code = row[DShop.CODE] as string;
            tmp.name = row[DShop.NAME] as string;
            tmp.address = row[DShop.ADDRESS] as string;
            tmp.postcode = row[DShop.POSTCODE] as string;
            tmp.contact = row[DShop.CONTACT] as string;
            tmp.telephone = row[DShop.TELEPHONE] as string;
            tmp.cellphone = row[DShop.CELLPHONE] as string;
            tmp.status = CPos.STATUE_ENABLED;
            obj.shop = tmp;

            return tmp;
        }

        private bool ReadTerminal(RDBEngine dba, string corp, string shop, string code, string addr, MTerminal obj)
        {
            dba.AddTable(DTerminal.TABLE);
            dba.AddWhere(DTerminal.CORP_ID, corp);
            dba.AddWhere(DTerminal.SHOP_ID, shop);
            dba.AddWhere(DTerminal.CODE, code);
            dba.AddWhere(DTerminal.STATUS, CTerminal.STATUE_ENABLED.ToString(), false);

            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count < 1)
            {
                obj.error_code = PObj.ERROR_CODE_CUSTOM;
                obj.error_message = "无效的终端代码！";
                return false;
            }

            // 服务器不指定IP时，表示不限制客户IP
            var row = dt.Rows[0];
            var tmp = row[DTerminal.ADDR] as string;
            if (!string.IsNullOrWhiteSpace(tmp) && addr != tmp)
            {
                obj.error_code = PObj.ERROR_CODE_CUSTOM;
                obj.error_message = "无效的终端地址！";
                return false;
            }

            obj.id = row[DTerminal.ID] as string;
            obj.code = code;
            obj.addr = addr;
            obj.update_time = (DateTime)row[DTerminal.UPDATE_TIME];
            obj.create_time = (DateTime)row[DTerminal.CREATE_TIME];
            return true;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}